home *** CD-ROM | disk | FTP | other *** search
/ Games of Daze / Infomagic - Games of Daze (Summer 1995) (Disc 1 of 2).iso / x2ftp / msdos / lang / rcs567pc / rcsfront / cio.c < prev    next >
C/C++ Source or Header  |  1994-11-16  |  52KB  |  1,767 lines

  1. /*----------------------------------------------------------------------------
  2. / cio.c - This is a C program to replace the over-grown shell script
  3. / that started as something small...
  4. /
  5. / Authors:    Jason P. Winters and Craig J. Kim
  6. /
  7. /  We hereby declare this software to be Public Domain.  That means we don't
  8. /  care what you do with it.  We _would_ prefer that you at least leave our
  9. /  names in it, as befits Authors, but we can't force you.  Of course, since
  10. /  it's public domain, all risks/gains using it are your problems.  You don't
  11. /  have any reason to call us up and complain about it deleting 30megs of
  12. /  your source code without telling you. ( Software should at least *tell* you
  13. /  when it does something like that, right? )  :)
  14. /
  15. / Start Date:    November 23, 1988
  16. / Revisions:    29-Nov-88 jpw - initial coding completed.
  17. /        29-Nov-88 cjk - rewrite of front-end logic
  18. /        30-Nov-88 jpw - Added signals, cleanup of temp files.
  19. /        30-Nov-88 cjk - added -N option
  20. /        01-Dec-88 cjk - added usage
  21. /        01-Dec-88 jpw - added SUID controls for secure files.
  22. /        02-Dec-88 jpw - added -A option
  23. /        05-Dec-88 cjk - added '~' commands for message entering
  24. /        05-Dec-88 cjk - fixed cp to check file status before copying
  25. /        06-Dec-88 cjk - use separate process for user file input
  26. /        06-Dec-88 jpw - source now passes System V.2 lint
  27. /        07-Dec-88 cjk - added environment variable check routine
  28. /        20-Dec-88 cjk - chmod files in source dir to 0640
  29. /        21-Dec-88 cjk - put RCS header if not exists
  30. /        08-Mar-89 cjk - SCCS version
  31. /        02-Oct-89 jpw - Fixed parsing for header type to insert
  32. /                        Fixed small bug in directory creation routine
  33. /        10-May-90 jpw - Changed st += sprintf() code to allow for
  34. /                        broken sprint() calls.  Failed on Sun
  35. /                        machines.
  36. /
  37. / Known bugs:
  38. /       On some systems, the Control-D as end of input in the log entry
  39. /       routines will cause stdin to be closed, which means the next call
  40. /       to get an entry (such as a Title file) will fail.  A call to clearerr()
  41. /       has been added to fix this, but it has not been tested.
  42. /
  43. / To be done:
  44. /    1. When -U is used, the destination directory should be created if
  45. /       it does not exist already.  Also, the file mode should be changed to
  46. /       0640 so that overwriting is not possible by anyone other than
  47. /       $RCSOWN for security.
  48. /    2. Full "interactive" mode support
  49. /   3. Actually use the SCCS version. (Ugh!)
  50. /---------------------------------------------------------------------------*/
  51.  
  52. /*#define DEBUG        /* wanna know what's goin' on? */
  53. /*#define INTERACTIVE    /* enable -I option (incomplete) */
  54. /*#define void    int    /* if system can't handle void types. */
  55. #define V_RCS        /* RCS version */
  56. /*#define V_SCCS        /* SCCS version */
  57.  
  58. /*--------------------------------------------------- includes -------------*/
  59. #include <stdio.h>
  60. #include <string.h>
  61. #include <ctype.h>
  62. #include <sys/types.h>
  63. #include <sys/stat.h>
  64. #include <errno.h>
  65. #include <signal.h>
  66. #include <fcntl.h>
  67.  
  68. #ifdef __EMX__
  69. #define getcwd _getcwd2
  70. #define mkdir(d) mkdir (d, 0755)
  71. #endif
  72.  
  73. #ifndef OS2
  74. #define stricmp   strcmp
  75. #define strnicmp  strncmp
  76. #endif
  77.  
  78. /*--------------------------------------------------- externals ------------*/
  79. extern FILE *fopen();               /* open a stream (should be in stdio.h) */
  80. extern FILE *popen();                 /* open a pipe (should be in stdio.h) */
  81. extern char *getenv();                      /* read an environment variable */
  82. extern char *tmpnam();                      /* create a temporary file name */
  83. extern char *mktemp();                  /* create temp file with a template */
  84. extern char *malloc();                        /* allocate a chunk of memory */
  85. extern char *getcwd();                             /* get current directory */
  86. extern int  unlink();                             /* unlink (delete) a file */
  87. extern void exit();                            /* define these for LINT!!!! */
  88. extern void perror();                          /* define these for LINT!!!! */
  89. extern void free();                            /* define these for LINT!!!! */
  90.  
  91. /*--------------------------------------------------- defines --------------*/
  92. #ifndef TRUE
  93. # define TRUE    1
  94. # define FALSE    0
  95. #endif
  96.  
  97. #define TYPE_LOG    0        /* log message */
  98. #define TYPE_TITLE    1        /* title message */
  99. #define MAX_LOG        1020        /* max # of characters for a log */
  100.  
  101. /*--------------------------------------------------- forwards -------------*/
  102. void usage(/*_ void _*/);                    /* print program usage message */
  103. int  getfinput(/*_ char *name, int type _*/);             /* get title file */
  104. int  child_getfinput(/*_ char *name, int type _*/);      /* actual get file */
  105. void doincmds(/*_ char *argv[], int argc, int in _*/); /* perform user wish */
  106. void do_ciodir(/*_ char *filenm _*/);    /* do user wish in recursive if -R */
  107. int  cio(/*_ char *filenme _*/);                          /* the work horse */
  108. int  addrcsext(/*_ char *fname _*/);         /* add RCS file name extension */
  109. int  rmrcsext(/*_ char *fname _*/);       /* remove RCS file name extension */
  110. void inshdr(/*_ char *fname _*/);                      /* insert RCS header */
  111. int  makedir(/*_ char *path _*/);    /* make a directory and all its parent */
  112. char *strstr(/*_ char *s1, char *s2 _*/);  /* find a string within a string */
  113. char *strlwr(/*_ char *s _*/);               /* convert upper case to lower */
  114. int  asciifile(/*_ char *filename _*/);                  /* check file type */
  115. int  rcsfile(/*_ char *filename _*/);             /* check file is RCS file */
  116. int  strrd(/*_ char *buf, int max_size,  FILE *fle _*/);/* read from a file */
  117. void getdir(/*_ void _*/);        /* obtain necessary directory information */
  118. int  fix_envstr(/*_ char *cs _*/);  /* remove leading/trailing blanks, etc. */
  119. void getrcsdir(/*_ char *dir _*/);          /* build rcs dir out of working */
  120. void getworkdir(/*_ char *dir _*/);         /* build working dir out of rcs */
  121. void getsrcdir(/*_ char *dir _*/);       /* build source dir out of working */
  122. char *justname(/*_ char *fpath _*/);        /* return just filename portion */
  123. char *memalloc(/*_ int size _*/);  /* allocate memory and check for success */
  124. void sigcleanup(/*_ void _*/);                 /* cleanup in case interrupt */
  125. void get_final_id(/*_ void _*/);           /* get user id of RCS file owner */
  126. int  nextent(/*_ FILE *fp _*/);    /* read next entry from /etc/passwd file */
  127.  
  128. /*--------------------------------------------------- globals --------------*/
  129. #ifdef V_RCS
  130. char *ci_cmd = "ci";                /* command to use to check in a module. */
  131. char *co_cmd = "co";               /* command to use to check out a module. */
  132. #else
  133. char *ici_cmd = "admin";      /* command to check in a module for 1st time. */
  134. char *ci_cmd = "delta";                    /* command to check in a module. */
  135. char *co_cmd = "get";                     /* command to check out a module. */
  136. #endif
  137. char *currentdir;                                     /* current directory. */
  138. char *homedir;                                    /* user's home directory. */
  139. char *rcswrk;                                                  /* $RCSWORK. */
  140. char *rcsown;                                                   /* $RCSOWN. */
  141. char *rcsdir;                                                   /* $RCSDIR. */
  142. char *srcdir;                                                   /* $RCSSRC. */
  143. char *headdir;                                                 /* $RCSHEAD. */
  144. char *path;                                                       /* $PATH. */
  145. char *pwdfile = "/etc/passwd";                      /* default passwd file. */
  146. char cioopt[100];                      /* large buffer for command options. */
  147. char d_ent[90];                             /* small buffer for file reads. */
  148. char final[400];                    /* final dir to pass on as destination. */
  149. #ifdef V_SCCS
  150. char finalfile[400];                  /* final file without SCCS extension. */
  151. #endif
  152. char logstr[MAX_LOG + 4];                            /* log string to pass. */
  153. char title[100];                                     /* log string to pass. */
  154. char cmdbuf[2400];                                     /* do a single file. */
  155. char editfile[400];                  /* temp edit file, makes cleanup easy. */
  156. char pwdname[20];                                    /* Name found in file. */
  157. char ftypestr[82];                               /* command file(1) output. */
  158. char cii = FALSE;                       /* if set, we are in check in mode. */
  159. char recurse = FALSE;               /* if set, do recursive check in/out's. */
  160. char usertitle = FALSE;                       /* user specified title file. */
  161. char interactive = FALSE;                /* user friendly interactive mode. */
  162. char allfiles = FALSE;                    /* set it TRUE to copy all files. */
  163. char insertheader = FALSE;     /* insert RCS header at the top of the file. */
  164. #ifdef DEBUG
  165. char noexec = TRUE;                     /* set it FALSE for execution mode. */
  166. char verbose = TRUE;                                    /* be a chatterbox. */
  167. #else
  168. char noexec = FALSE;                  /* set it TRUE for no execution mode. */
  169. char verbose = FALSE;                                  /* not a chatterbox. */
  170. #endif
  171. char updsrcdir = FALSE;           /* update master source directory on cii. */
  172. #ifdef V_SCCS
  173. char do_admin = FALSE;                    /* set it TRUE if first check in. */
  174. #endif
  175. char *prognam;                                     /* name of this program. */
  176. struct stat filestat;                                  /* file status info. */
  177. int s_currentdir;                           /* length of current directory. */
  178. int s_homedir;                          /* length of user's home directory. */
  179. int s_rcswrk;                                        /* length of $RCSWORK. */
  180. int s_rcsdir;                                         /* length of $RCSDIR. */
  181. int s_srcdir;                                         /* length of $RCSSRC. */
  182. int s_path;                                             /* length of $PATH. */
  183. int s_rcsown;                                         /* length of $RCSOWN. */
  184. int s_headdir;                                       /* length of $RCSHEAD. */
  185. int user_id;                   /* Effective user id to use if ROOT process. */
  186. int real_user_id;                      /* Save buffer for original user_id. */
  187. int do_unlink;  /* add -l option if not set to avoid unlink of source file. */
  188.  
  189. /*--------------------------------------------------- main() ----------------
  190. / where we begin!
  191. /---------------------------------------------------------------------------*/
  192. main(argc, argv)
  193. int argc;
  194. char *argv[];
  195. {
  196.     register int in;
  197.     register char *cp;        /* used in string updates. */
  198.  
  199. #ifdef __EMX__
  200.     _response(&argc, &argv);
  201.     _wildcard(&argc, &argv);
  202.     _emxload_env("RCSLOAD");
  203. #endif
  204.  
  205.     prognam = justname(argv[0]);    /* program name */
  206. #ifdef OS2
  207.         if ( stricmp(prognam + 3, ".exe") == 0 )
  208.           prognam[3] = 0;
  209. #endif
  210.     getdir();            /* go get the enviroment pointers. */
  211.  
  212.     /*
  213.      * figure what the user wants us to be by reading program name
  214.      */
  215.     if (!stricmp("ciitest", prognam) || !stricmp("cootest", prognam))
  216.     {
  217.         getrcsdir(final, currentdir);    /* setup variables. */
  218.         /*
  219.          * print out our variables here.
  220.         */
  221. #ifdef V_RCS
  222.         (void) printf("Homedir:%s:  rcsdir:%s:  rcswrk:%s:  rcssrc:%s:\n",
  223. #else
  224.         (void) printf("HOME:%s:  SCCSDIR:%s:  SCCSWRK:%s:  SCCSSRC:%s:\n",
  225. #endif
  226.                 homedir ? homedir : "",
  227.                 rcsdir ? rcsdir : "",
  228.                 rcswrk ? rcswrk : "",
  229.                 srcdir ? srcdir : "");
  230.         (void) printf("Final dir:%s:\n", final);
  231.         return(0);
  232.     }
  233.     if (!stricmp("cii", prognam))    /* this is input.. */
  234.         cii = TRUE;        /* show we are inputs. */
  235.     else if (stricmp("coo", prognam))/* it's not this either.. */
  236.     {
  237.         (void) printf("Just what did you think this program was, anyway??\n");
  238.         return(-1);
  239.     }
  240.  
  241. #ifndef OS2        
  242.     if (!(user_id = geteuid())) /* we are a root process.. */
  243.     {
  244.         (void) umask(027); /* set general mask to private modes. */
  245.         real_user_id = getuid();
  246.         if (cii)    /* only set this if we are in checkin mode. */
  247.             get_final_id(); /* go get the final user ID for file creation. */
  248.         else
  249.             user_id = real_user_id;    /* otherwise, use owners real one. */
  250.         (void) setuid(user_id); /* and, fix up the user id now. */
  251. #ifdef DEBUG
  252.         (void) printf("Using Uid:%d:\n", user_id);
  253. #endif
  254.     }
  255.     else
  256.         real_user_id = user_id;    /* else they should match. */
  257. #else 
  258.     real_user_id = 1;    /* else they should match. */
  259. #endif
  260.  
  261.     /* prepare for disasters */
  262.     (void) signal(SIGINT, sigcleanup);
  263. #ifndef OS2
  264.     (void) signal(SIGQUIT, sigcleanup);
  265. #else
  266.     (void) signal(SIGBREAK, sigcleanup); /* OS/2 */
  267.     (void) signal(SIGTERM, sigcleanup);  /* signals */
  268. #endif
  269.  
  270.         if ( argc <= 1 )
  271.         {
  272.             usage();
  273.             return(0);
  274.         }
  275.  
  276.     cp = cioopt;
  277.     title[0] = '\0';  /* make sure no titles are required. */
  278.     for (in = 1; argv[in][0] == '-'; in++) /* while chars here.. */
  279.     {
  280.         switch ((int) argv[in][1])    /* what option? */
  281.         {
  282.         case '?':        /* print program usage */
  283.         case '-':
  284.             usage();
  285.             return(0);
  286.         case 'A':        /* copy all files */
  287.             allfiles = !allfiles;
  288.             break;
  289. #ifdef INTERACTIVE
  290.         case 'I':        /* interactive mode */
  291.             interactive = !interactive;
  292.             break;
  293. #endif
  294.         case 'H':        /* RCS/SCCS header */
  295.             insertheader = !insertheader;
  296.             break;
  297.         case 'N':        /* do not execute */
  298.             noexec = !noexec;
  299.             break;
  300.         case 'R':        /* Recursion flag. */
  301.             recurse = !recurse;
  302.             break;
  303.         case 'T':        /* get Title flag. */
  304.             if(cii)    /* get a title file and name. */
  305.                 (void) getfinput(title, TYPE_TITLE);
  306.             break;
  307.         case 'U':        /* update working directory */
  308.             updsrcdir = !updsrcdir;
  309.             break;
  310.         case 'V':        /* verbose */
  311.             verbose = !verbose;
  312.             break;
  313. #ifdef V_RCS
  314.         case 'm':        /* they gave us one */
  315. #else
  316.         case 'y':        /* they gave us one */
  317. #endif
  318.             (void) strcpy(logstr, " -");
  319.             (void) strcat(logstr, &argv[in][1]); /* copy across. */
  320.             break;
  321.         case 't':        /* user gave us one */
  322.             (void) strcpy(title, &argv[in][1]);
  323.             usertitle = TRUE;
  324.             break;
  325.         default:        /* must be a ci or co command */
  326.             (void) sprintf(cp, " %s", argv[in]); /* append */
  327.             cp += strlen(cp); /* skip to end of string. */
  328.             break;
  329.         }
  330.     }
  331. #ifdef INTERACTIVE
  332.     if (noexec && interactive)    /* resolve conflict of interest */
  333.         interactive = FALSE;
  334. #endif
  335.     doincmds(argv, argc, in);    /* do the check in command. */
  336.     if (title[0] && !usertitle)    /* if file is here. */
  337.         (void) unlink(title);    /* zap it! */
  338.     return(0);            /* we did it good! */
  339. }
  340.  
  341. /*--------------------------------------------------- usage() ---------------
  342. / print program usage information
  343. /---------------------------------------------------------------------------*/
  344. void
  345. usage()
  346. {
  347.     char *inout = cii ? "in" : "out";
  348.  
  349. #ifdef INTERACTIVE
  350.     (void) fprintf(stderr, "Usage: %s [-A] %s[-I] [-N] [-R] %s",
  351. #else
  352.     (void) fprintf(stderr, "Usage: %s [-A] %s[-N] [-R] %s",
  353. #endif
  354.             prognam, cii ? "[-H] " : "",
  355.                  cii ? "[-T] [-U] " : "");
  356.     (void) fprintf(stderr, "[-V] [%s options] [[filename]...]\n",
  357.             cii ? ci_cmd : co_cmd);
  358.     (void) fprintf(stderr, "     -A : check %s all files\n", inout);
  359. #ifdef INTERACTIVE
  360.     (void) fprintf(stderr, "     -I : interactive mode\n");
  361. #endif
  362.     if (cii)
  363. #ifdef V_RCS
  364.         (void) fprintf(stderr, "     -H : attach RCS header\n");
  365. #else
  366.         (void) fprintf(stderr, "     -H : attach SCCS header\n");
  367. #endif
  368.     (void) fprintf(stderr, "     -N : no execute mode\n");
  369.     (void) fprintf(stderr, "     -R : recursive check %s\n", inout);
  370.     if (cii)
  371.     {
  372.         (void) fprintf(stderr, "     -T : create title file\n");
  373.         (void) fprintf(stderr, "     -U : update source directory\n");
  374.     }
  375.     (void) fprintf(stderr, "     -V : verbose mode\n");
  376. }
  377.  
  378. /*--------------------------------------------------- sigcleanup() ----------
  379. / cleanup before exiting.
  380. /---------------------------------------------------------------------------*/
  381. void
  382. sigcleanup()
  383. {
  384.     (void) printf("\n[%s: Interrupted]\n", prognam);
  385.     if (title[0] && !usertitle)    /* remove title file, if here. */
  386.         (void) unlink(title);
  387.     if (editfile[0])        /* if a temp file might be here. */
  388.         (void) unlink(editfile);    /* attempt to remove it.*/
  389.     exit(0);
  390. }
  391.  
  392. /*--------------------------------------------------- doincmds() ------------
  393. / actual do routine
  394. /---------------------------------------------------------------------------*/
  395. void
  396. doincmds(argv, argc, in)
  397. char *argv[];
  398. int argc, in;
  399. {
  400.     register char *cp;
  401.     char entry[400];
  402.  
  403.     if (cii)
  404.     {
  405.         if (argc == in)        /* no arguments given */
  406.         {
  407.             do_ciodir(currentdir);
  408.             return /* void */;
  409.         }
  410.         for ( ; in < argc; in++)
  411.         {
  412. #ifdef OS2
  413.                         UnixFileName(argv[in]);
  414. #endif
  415.             (void) sprintf(entry, "%s%s", currentdir, argv[in]);
  416. #ifdef DEBUG
  417.             (void) printf("Processing %s\n", entry);
  418. #endif
  419.             if (stat(entry, &filestat))
  420.             {
  421. #ifdef DEBUG
  422.                 (void) printf("Unable to stat(2) %s\n", entry);
  423. #endif
  424.                 continue;
  425.             }
  426.             do_unlink = (filestat.st_uid == real_user_id);
  427.             if ((filestat.st_mode & S_IFMT) == S_IFDIR)
  428.                 do_ciodir(entry);
  429.             else
  430.                 (void) cio(entry);
  431.         }
  432.     }
  433.     else /* coo */
  434.     {
  435.         char *ep;
  436.  
  437.         getrcsdir(entry, currentdir);
  438.         if (argc == in)
  439.         {
  440.             do_ciodir(entry);
  441.             return /* void */;
  442.         }
  443.         for (ep = entry; *ep; ep++)
  444.             ;
  445.         for ( ; in < argc; in++)
  446.         {
  447. #ifdef OS2
  448.                         UnixFileName(argv[in]);
  449. #endif
  450.             (void) sprintf(ep, "%s", argv[in]);
  451. #ifdef DEBUG
  452.             (void) printf("Processing %s\n", entry);
  453. #endif
  454.             if (stat(entry, &filestat))
  455.             {
  456.                 if (!addrcsext(entry))
  457.                     continue;
  458.                 if (stat(entry, &filestat))
  459.                     continue;
  460.             }
  461.             do_unlink = (filestat.st_uid == real_user_id);
  462.             if ((filestat.st_mode & S_IFMT) == S_IFDIR)
  463.                 do_ciodir(entry);
  464.             else
  465.                 (void) cio(entry);
  466.         }
  467.  
  468.     }
  469.     return /* void */;
  470. }
  471.  
  472. /*--------------------------------------------------- do_ciodir() -----------
  473. / actual do routine - Warning: RECURSIVE
  474. /---------------------------------------------------------------------------*/
  475. void
  476. do_ciodir(dir)
  477. char *dir;
  478. {
  479.     FILE *pp;
  480.     char *cmd, *entry;
  481.  
  482.         if ( dir[strlen(dir) - 1] == '/' )
  483.           dir[strlen(dir) - 1] = 0;
  484. #ifdef OS2
  485.     (void) sprintf(cmd = memalloc(strlen(dir) + 10), "dir /b \"%s\"", dir);
  486. #else
  487.     (void) sprintf(cmd = memalloc(strlen(dir) + 4), "ls %s", dir);
  488. #endif
  489.     pp = popen(cmd, "r");
  490.     free(cmd);
  491.     if (!pp)
  492.         return /* void */;
  493.     entry = memalloc(strlen(dir) + 30);
  494.     while (strrd(d_ent, 80, pp) != -1)
  495.     {
  496.         (void) sprintf(entry, "%s/%s", dir, d_ent);
  497.         if (stat(entry, &filestat))
  498.         {
  499. #ifdef DEBUG
  500.             (void) printf("Unable to stat(2) %s\n", entry);
  501. #endif
  502.             continue;
  503.         }
  504.         do_unlink = (filestat.st_uid == real_user_id);
  505.         if ((filestat.st_mode & S_IFMT) == S_IFDIR)
  506.         {
  507.             if (recurse)
  508.                 do_ciodir(entry);
  509.             else
  510.                 continue;
  511.         }
  512.         else
  513.             (void) cio(entry);
  514.     }
  515.     free(entry);
  516.     (void) pclose(pp);
  517.     return /* void */;
  518. }
  519.  
  520. /*--------------------------------------------------- cio() -----------------
  521. / do this to file, if we can.
  522. /---------------------------------------------------------------------------*/
  523. int
  524. cio(filename)
  525. char *filename;
  526. {
  527.     register char *cp, *st;        /* some char pointers */
  528.     int do_copy = FALSE;        /* do cp(1) */
  529.     char titlest[100];        /* for file names. */
  530.  
  531.     if (cii)
  532.     {
  533.         if (!asciifile(filename))    /* if not ascii file */
  534.         {
  535.             if (!allfiles)
  536.                 return(FALSE);    /* don't if not forced copy */
  537.             do_copy = TRUE;
  538.         }
  539.         else if (insertheader)        /* inserting default header */
  540.         {
  541.             inshdr(filename);
  542.         }
  543.     }
  544.     else if (!rcsfile(filename))        /* not an RCS file */
  545.     {
  546.         if (!allfiles)
  547.             return(FALSE);
  548.         do_copy = TRUE;            /* simply cp(1) it */
  549.     }
  550.  
  551.     if (cii && !do_copy && !logstr[0]) /* we don't have a log entry yet. */
  552.     {
  553.         if (noexec)
  554.         {
  555.             (void) printf("Logfile entry bypassed.\n");
  556.             (void) strcpy(logstr, "");    /* fake it */
  557.         }
  558.         else if (!getfinput(titlest, TYPE_LOG)) /* if we entered anything */
  559.         {
  560.             FILE *fp;
  561.             char buf[100];
  562.             register char *bp;
  563.             register int numchars = 0;
  564.  
  565.             if (fp = fopen(titlest, "r"))
  566.             {
  567.                 st = logstr;
  568. #ifdef V_RCS
  569.                 (void) sprintf(st, " -m\"");
  570. #else
  571.                 (void) sprintf(st, " -y\"");
  572. #endif
  573.                 st += strlen(st); /* skip to end of string. */
  574.                 while (strrd(buf, 80, fp) != -1)
  575.                 {    /*
  576.                      * escape quotes (") characters
  577.                      */
  578.                     bp = buf;
  579.                     while (*bp)
  580.                     {
  581.                         if (*bp == '"')
  582.                             if (numchars < MAX_LOG)
  583.                             {
  584.                                 numchars++;
  585.                                 *st++ = '\\'; /*escape*/
  586.                             }
  587.                         if (numchars < MAX_LOG)
  588.                         {
  589.                             numchars++;
  590.                             *st++ = *bp++; /* append */
  591.                         }
  592.                     }
  593.                     *st++ = '\n'; /* add end of line now. */
  594.                 }
  595.                 if (*(st - 1) == '\n')
  596.                     *(st - 1) = '"';
  597.                 else
  598.                     *st++ = '"';
  599.                 *st = '\0';
  600.                 (void) fclose(fp);
  601.                 if (numchars >= MAX_LOG)
  602.                     (void) printf("Log entry truncated.\n");
  603.             }
  604.             (void) unlink(titlest); /* cleanup. */
  605.         }
  606.     }
  607.  
  608.     titlest[0] = '\0'; /* cleanup string reference. */
  609.     if (cii)
  610.         getrcsdir(final, filename);
  611.     else
  612.         getworkdir(final, filename);
  613.  
  614.     if (st = strrchr(final, '/'))    /* if this has a sub dir. */
  615.     {
  616.         *st = '\0';    /* terminate it at the directoy level. */
  617.         if (access(final, 0))    /* it's not here... */
  618.             if (!makedir(final))    /* so try and make it. */
  619.             {
  620.                 (void) printf("Could not create directory: %s:\n", final);
  621.                 return(FALSE);
  622.             }
  623.         *st = '/';    /* restore the rest of the file name. */
  624.     }
  625.  
  626.     if (cii)
  627.     {
  628.         if (!do_copy)    /* not in binary mode. */
  629.         {    /*
  630.              * make the new file name and check if it's here
  631.              */
  632. #ifdef V_SCCS
  633.             (void) strcpy(finalfile, final);
  634. #endif
  635.             (void) addrcsext(final);
  636.             if (!noexec && access(final, 0))
  637.             {
  638.                 /*
  639.                  * if not, need to get a title message for it
  640.                  */
  641.                 if (!title[0]) /* no title file yet. */
  642.                     (void) getfinput(title, TYPE_TITLE); /* get one */
  643.                 if (title[0])  /* if we've one, build command */
  644.                     (void) sprintf(titlest, " -t%s ", title);
  645. #ifdef V_SCCS
  646.                 do_admin = TRUE;    /* 1st check-in */
  647. #endif
  648.             }
  649. #ifdef V_SCCS
  650.             else
  651.                 do_admin = FALSE;
  652. #endif
  653.         }
  654.         else if (noexec)
  655.             (void) strcpy(titlest, " -tfile_name ");
  656.     }
  657.     else    /* check out mode. */
  658.     {    /*
  659.          * find a comma to make the new name.
  660.          * if found one, strip it to make a new name.
  661.          */
  662.         (void) rmrcsext(final);
  663.     }
  664.  
  665.     /*
  666.      * Build command string
  667.      */
  668.     if (do_copy)    /* we want to just copy the file now. */
  669.     {
  670.         if (!interactive && !noexec && !access(final, 0))
  671.         {
  672.             (void) printf("%s already exists.  Overwrite? (yes) ", final);
  673.             (void) strrd(cmdbuf, 20, stdin);
  674.             (void) strlwr(cmdbuf);
  675.             if (strnicmp(cmdbuf, "yes", strlen(cmdbuf)))
  676.                 return(TRUE);
  677.         }
  678.         (void) sprintf(cmdbuf, "cp -p %s %s", filename, final); /* copy command */
  679.     }
  680.     else if (cii)
  681.     {
  682. #ifdef V_RCS
  683.         (void) sprintf(cmdbuf, "%s%s%s%s%s %s %s",
  684.             ci_cmd, cioopt, titlest, logstr,
  685.             do_unlink ? "" : " -l", filename, final);
  686. #else
  687.         st = cmdbuf;
  688.         (void) sprintf(st, "cp %s %s; ", filename, finalfile);
  689.         st += strlen(st); /* skip to end of string. */
  690.         if (cp = strrchr(final, '/'))
  691.         {
  692.             *cp = '\0';
  693.             (void) sprintf(st, "cd %s; ", final);
  694.             st += strlen(st); /* skip to end of string. */
  695.             *cp = '/';
  696.         }
  697.         if (do_admin)
  698.         {
  699.             (void) sprintf(st, "%s -i%s%s%s%s %s",
  700.                 ici_cmd, justname(finalfile),
  701.                 cioopt, titlest, logstr, justname(final));
  702.             st += strlen(st); /* skip to end of string. */
  703.         }
  704.         else
  705.         {
  706.             (void) sprintf(st, "%s%s%s %s",
  707.                 ci_cmd, cioopt, logstr, justname(final));
  708.             st += strlen(st); /* skip to end of string. */
  709.         }
  710.         if (do_unlink)
  711.         {
  712.             (void) sprintf(st, "; rm %s", filename);
  713.             st += strlen(st); /* skip to end of string. */
  714.         }
  715. #endif
  716.     }
  717.     else    /* coo */
  718.     {
  719. #ifdef V_RCS
  720.         (void) sprintf(cmdbuf, "%s%s %s %s",
  721.                 co_cmd, cioopt, filename, final);
  722. #else
  723.         st = cmdbuf;
  724.         (void) sprintf(st, "rm -f %s; ", final);
  725.         st += strlen(st); /* skip to end of string. */
  726.         if (cp = strrchr(filename, '/'))
  727.         {
  728.             *cp = '\0';
  729.             (void) sprintf(st, "cd %s; ", filename);
  730.             st += strlen(st); /* skip to end of string. */
  731.             *cp = '/';
  732.         }
  733.         addrcsext(filename);
  734.         (void) sprintf(st, "%s%s %s", co_cmd, cioopt,
  735.             justname(filename));
  736.         st += strlen(st); /* skip to end of string. */
  737.         if (cp = strrchr(final, '/'))
  738.         {
  739.             cp++;
  740.             (void) sprintf(st, "; mv %s %s", cp, final);
  741.             st += strlen(st); /* skip to end of string. */
  742.         }
  743. #endif
  744.     }
  745.  
  746. #ifdef INTERACTIVE
  747.     if (interactive)
  748.     {
  749.         char ans[20];
  750.         int done = FALSE;
  751.  
  752.         do
  753.         {
  754.             (void) printf("%s\nExecute? (yes) ", cmdbuf);
  755.             (void) strrd(ans, 20, stdin);
  756.             st = ans;
  757.             while (isspace(*st))
  758.                 st++;
  759.             if (!*st)
  760.                 (void) strcpy(st, "yes");
  761.             (void) strlwr(st);
  762.             if (!strnicmp(st, "yes", strlen(st))) {
  763.                 done = TRUE;
  764.                 (void) system(cmdbuf);    /* do it. */
  765.             }
  766.             else if (*st == '?')
  767.             {
  768.                 (void) printf("\n");
  769.                 (void) printf("yes     Do it\n");
  770.                 (void) printf("no      Don't do it\n");
  771.                 (void) printf("view    View current file\n");
  772.                 (void) printf("?       Print this message\n");
  773.                 (void) printf("\n");
  774.             }
  775.             else if (!strnicmp(st, "view", strlen(st)))
  776.             {
  777.                 (void) printf("Not implemented yet!\n\n");
  778.             }
  779.             else /* take it as "no" */
  780.                 done = TRUE;
  781.         } while (!done);
  782.     }
  783.     else
  784.     {
  785. #endif /* INTERACTIVE */
  786.         if (verbose)
  787.             (void) printf("%s command: %s:\n", prognam, cmdbuf);
  788.         if (!noexec)
  789.         {
  790.             if (do_copy && !verbose)
  791.                 (void) printf("%s\n", cmdbuf);
  792.             (void) system(cmdbuf);    /* do it. */
  793.         }
  794.         else if (!verbose)    /* if don't want to exec, don't */
  795.             (void) printf("%s\n", cmdbuf);
  796. #ifdef INTERACTIVE
  797.     }
  798. #endif /* INTERACTIVE */
  799.  
  800.     if (updsrcdir)            /* update source directory */
  801.     {
  802.         getsrcdir(final, filename);
  803.  
  804.             if (st = strrchr(final, '/'))    /* if this has a sub dir. */
  805.             {
  806.                 *st = '\0';    /* terminate it at the directoy level. */
  807.                 if (access(final, 0))    /* it's not here... */
  808.                     if (!makedir(final))    /* so try and make it. */
  809.                     {
  810.                         (void) printf("Could not create directory: %s:\n", final);
  811.                         return(FALSE);
  812.                     }
  813.                 *st = '/';    /* restore the rest of the file name. */
  814.             }
  815.  
  816.         (void) sprintf(cmdbuf, "cp -p %s %s", filename, final);
  817.         if (noexec)        /* don't actual do it */
  818.             (void) printf("%s\n", cmdbuf);
  819.         else
  820.         {
  821.             if (verbose)    /* speak, yo wise one! */
  822.                 (void) printf("%s command: %s:\n", prognam, cmdbuf);
  823.             if (!stricmp(filename, final))
  824.                 (void) printf("Source and destination identical.  Not updated.\n");
  825.             else
  826.             {
  827.                 (void) chmod(final, 0640);
  828.                 (void) system(cmdbuf);    /* do it! */
  829.             }
  830.         }
  831.     }
  832.     return(TRUE);
  833. }
  834.  
  835. /*--------------------------------------------------- addrcsext() -----------
  836. / add RCS file extension ",v" if there isn't one already
  837. /---------------------------------------------------------------------------*/
  838. int
  839. addrcsext(fname)
  840. char *fname;
  841. {
  842.     register char *cp;
  843.  
  844. #ifdef V_RCS
  845.     for (cp = fname; *cp; cp++)
  846.         ;
  847.     if (*--cp == 'v' && *(cp - 1) == ',')
  848.         return(0);        /* already there */
  849.  
  850.     *++cp = ',';            /* add ",v" */
  851.     *++cp = 'v';
  852.     *++cp = '\0';
  853. #else
  854.     char t_name[20];
  855.  
  856.     cp = justname(fname);
  857.     if (*cp == 's' && *(cp + 1) == '.')
  858.         return(0);
  859.     (void) strcpy(t_name, cp);
  860.     *cp++ = 's';            /* add "s." in front of file name */
  861.     *cp++ = '.';
  862.     (void) strcpy(cp, t_name);
  863. #endif
  864.     return(1);
  865. }
  866.  
  867. /*--------------------------------------------------- rmrcsext() ------------
  868. / remove RCS extension if there is one; returns 1 if remove, else 0
  869. /---------------------------------------------------------------------------*/
  870. int
  871. rmrcsext(fname)
  872. char *fname;
  873. {
  874.     register char *cp;
  875.  
  876. #ifdef V_RCS
  877.     for (cp = fname; *cp; cp++)
  878.         ;
  879.     if (*--cp == 'v' && *--cp == ',')
  880.     {
  881.         *cp = '\0';
  882.         return(1);
  883.     }
  884. #else
  885.     cp = justname(fname);
  886.     if (*cp == 's' && *(cp + 1) == '.')
  887.     {
  888.         (void) strcpy(cp, cp + 2);
  889.         return(1);
  890.     }
  891. #endif
  892.     return(0);
  893. }
  894.  
  895. /*--------------------------------------------------- inshdr() --------------
  896. / insert RCS header if none exists already
  897. /---------------------------------------------------------------------------*/
  898. void
  899. inshdr(t_name)
  900. char *t_name;
  901. {
  902. #    define FTYPE_C        0    /* C program text */
  903. #    define FTYPE_S        1    /* assembly program text */
  904. #    define FTYPE_SH        2    /* shell script */
  905. #    define FTYPE_ROFF    3    /* nroff, tbl, eqn, etc */
  906. #    define FTYPE_F        4    /* Fortran program text */
  907. #    define FTYPE_DEFAULT    5    /* don't know */
  908. #    define FTYPE_MK        6    /* makefile script */
  909. #    define FTYPE_H        7    /* C header file text */
  910.  
  911.     static struct _ftype {
  912.         char *keyword;        /* phrase may exist in file(1) output */
  913.         char *header;        /* header template file name. */
  914.     } ftype[] = {
  915. #ifdef V_RCS
  916.         { "c program",    "rcshead.c"    },    /* FTYPE_C */
  917.         { "assembler",    "rcshead.s"    },    /* FTYPE_S */
  918.         { "command",    "rcshead.sh"    },    /* FTYPE_SH */
  919.         { "roff, tbl",    "rcshead.rof"    },    /* FTYPE_ROFF */
  920.         { "fortran",    "rcshead.f"    },    /* FTYPE_F */
  921.         { 0,        "rcshead"    },    /* FTYPE_DEFAULT */
  922.         { 0,        "rcshead.mk"    },    /* FTYPE_MK */
  923.         { 0,        "rcshead.h"    } };    /* FTYPE_H */
  924. #else
  925.         { "c program",    "sccshead.c"    },    /* FTYPE_C */
  926.         { "assembler",    "sccshead.s"    },    /* FTYPE_S */
  927.         { "command",    "sccshead.sh"    },    /* FTYPE_SH */
  928.         { "roff, tbl",    "sccshead.rof"},    /* FTYPE_ROFF */
  929.         { "fortran",    "sccshead.f"    },    /* FTYPE_F */
  930.         { 0,        "sccshead"    },    /* FTYPE_DEFAULT */
  931.         { 0,        "sccshead.mk"    },    /* FTYPE_MK */
  932.         { 0,        "sccshead.h"    } };    /* FTYPE_H */
  933. #endif /* V_RCS */
  934.     static struct _fext {
  935.         char *name;
  936.         int type;
  937.     } fext[] = {
  938.         { ".c",        FTYPE_C        },
  939.         { ".h",        FTYPE_H        },
  940.         { ".s",        FTYPE_S        },
  941.         { ".f",        FTYPE_F        },
  942.         { ".man",    FTYPE_ROFF    },
  943.         { ".mk",    FTYPE_MK    },
  944.         { ".1",        FTYPE_ROFF    },
  945.         { ".2",        FTYPE_ROFF    },
  946.         { ".3",        FTYPE_ROFF    },
  947.         { ".4",        FTYPE_ROFF    },
  948.         { ".5",        FTYPE_ROFF    },
  949.         { ".6",        FTYPE_ROFF    },
  950.         { ".7",        FTYPE_ROFF    },
  951.         { ".8",        FTYPE_ROFF    },
  952.         { ".9",        FTYPE_ROFF    },
  953.         { 0,        0        } };
  954.     FILE *ifp, *ofp;
  955.     char buf[4096], headfile[128], tempfile[20], fname[40];
  956.     register int i, c, ft = FTYPE_DEFAULT, err=0;
  957.     register char *ext;
  958.  
  959.     strcpy(fname, justname(t_name)); /* copy over only name. */
  960.  
  961.     if (!(ifp = fopen(t_name, "r")))    /* quickly check for RCS header */
  962.         return;
  963.                     /* we are looking for "$Header" */
  964.                     /* within first 50 lines of the file */
  965.     for (i = 0; strrd(buf, 128, ifp) > 0 && i < 50; i++)
  966. #ifdef V_RCS
  967.         if (strstr(buf, "$Header"))
  968. #else
  969.         if (strstr(buf, "#ident"))
  970. #endif
  971.         {
  972.             (void) fclose(ifp);
  973.             if (verbose)
  974. #ifdef V_RCS
  975.                 (void) printf("%s already has a RCS header.\n",
  976. #else
  977.                 (void) printf("%s already has a SCCS header.\n",
  978. #endif /* V_RCS */
  979.                         t_name);
  980.             return;
  981.         }
  982.  
  983.     (void) fclose(ifp);
  984.                     /* examine file(1) output */
  985.     for (i = 0; ftype[i].keyword; i++)
  986.         if (strstr(ftypestr, ftype[i].keyword))
  987.         {
  988.             ft = i;
  989.             break;        /* found one */
  990.         }
  991.  
  992.     if (!ftype[i].keyword)        /* file(1) didn't help */
  993.     {                /* examine file extension */
  994.         if (ext = strrchr(fname, '.'))
  995.         {
  996.             for (i = 0; fext[i].name; i++)
  997.                 if (!stricmp(ext, fext[i].name))
  998.                     ft = fext[i].type;
  999.         }
  1000.         else
  1001.         {            /* check if makefile script */
  1002.             (void) strcpy(buf, fname);
  1003.             (void) strlwr(buf);
  1004.             if (!strcmp(buf, "makefile") || !strcmp(buf, "Makefile"))
  1005.                 ft = FTYPE_MK; /* If either of two fixed names.. */
  1006.         }
  1007.     }
  1008.     else if (ft == FTYPE_C)        /* see if source or header */
  1009.     {
  1010.         if ((ext = strrchr(fname, '.')) && !stricmp(ext, ".h"))
  1011.             ft = FTYPE_H;
  1012.     }
  1013.     if (verbose)            /* is this necessary */
  1014.         (void) printf("%s is type [%d]\n", fname, ft);
  1015.     if (noexec)            /* no execution mode */
  1016.         return;
  1017.  
  1018.     (void) sprintf(headfile, "%s%s", headdir, ftype[ft].header);
  1019.     if (!(ifp = fopen(headfile, "r")))
  1020.     {
  1021.         (void) printf("Unable to open header template file [%s]\n",
  1022.                 headfile);
  1023.         return;
  1024.     }
  1025.     /* build a tmp file in the same directory as old file. */
  1026.     strcpy(tempfile, t_name);
  1027.     ext = justname(tempfile); /* find end of path. */
  1028.     *ext = '\0'; /* and terminate path there. */
  1029.     (void) strcat(tempfile, "ctXXXXXX"); /* add tmp name */
  1030.     (void) mktemp(tempfile);        /* generate temp file name */
  1031.     if (!(ofp = fopen(tempfile, "w")))    /* open temp file */
  1032.     {
  1033.         (void) printf("Unable to open temporary file [%s]\n", tempfile);
  1034.         (void) fclose(ifp);
  1035.         return;
  1036.     }
  1037.     while ((c = fgetc(ifp)) != EOF)        /* copy header first */
  1038.         (void) fputc(c, ofp);
  1039.     (void) fclose(ifp);
  1040.     if (!(ifp = fopen(t_name, "r")))        /* open check-in file */
  1041.     {
  1042.         (void) printf("Unable to open [%s] for read\n", t_name);
  1043.         (void) fclose(ofp);
  1044.         (void) unlink(tempfile);
  1045.         return;
  1046.     }
  1047.     while ((c = fgetc(ifp)) != EOF)        /* append to temp file */
  1048.         if(fputc(c, ofp) == EOF)
  1049.             err=1; /* couldn't write error. */
  1050.     (void) fclose(ifp);            /* done */
  1051.     (void) fclose(ofp);
  1052.  
  1053. /* ok.  It's hard to make sure that everthing has gone well; if we unlink
  1054.    the src file and can't link the temp file, we could lose everthing.
  1055.    So, if copy fails, leave temp file alone, as it may be the only copy
  1056.    we have left!  If the unlinking the original fails, we can remove the
  1057.    copy, as we don't need it.
  1058. */
  1059.     if(!err && !unlink(t_name))    /* 'mv tempfile fname' */
  1060.     {           
  1061. #ifndef OS2                
  1062.         if(!link(tempfile, t_name)) /* 'cp tempfile t_name' */
  1063.             (void) unlink(tempfile);        /* 'rm tempfile' */
  1064.         else
  1065.             (void) printf("Link of %s and %s failed after removing %s.\n%s not removed.\n",
  1066.                 tempfile, t_name, t_name, tempfile);
  1067. #else
  1068.         if(!rename(tempfile, t_name)) /* 'cp tempfile t_name' */
  1069.             (void) printf("Rename of %s to %s failed after removing %s.\n%s not removed.\n",
  1070.                 tempfile, t_name, t_name, tempfile);
  1071. #endif
  1072.     }
  1073.     else
  1074.     {
  1075.         (void) unlink(tempfile);        /* 'rm tempfile' */
  1076.         (void) printf("Could not insert header into %s.  Copy failed.\n", t_name);
  1077.     }
  1078. }
  1079.  
  1080. /*--------------------------------------------------- makedir() -------------
  1081. / make a directory path, with recursion.
  1082. / returns TRUE if successful, FALSE otherwise.
  1083. /
  1084. / This really needs to be re-written.  It works, but that's all I can really
  1085. / say for it...  Hey, *I* don't have mkdir() calls!
  1086. /---------------------------------------------------------------------------*/
  1087. int
  1088. makedir(newpath)
  1089. char *newpath;        /* path name to make. */
  1090. {
  1091.     register char *st, *cp;
  1092.  
  1093.     if(!*newpath) return(FALSE); /* skip last directory attempt. */
  1094.     if (noexec)
  1095.     {
  1096.         (void) printf("mkdir: %s\n", newpath);
  1097.         return(TRUE);
  1098.     }
  1099.     else if(verbose)
  1100.         (void) printf("mkdir: %s\n", newpath);
  1101.  
  1102.     cp = memalloc(strlen(newpath) + 24);
  1103.  
  1104.     if (mkdir(newpath))            /* it failed.. */
  1105.     {
  1106.         (void) strcpy(cp, newpath);    /* get current one. */
  1107.         st = strrchr(cp, '/'); /* remove one more layer.. */
  1108.         *st = '\0';        /* terminate here. */
  1109.         if (makedir(cp) == FALSE)    /* try and build next level back. */
  1110.         {
  1111.             free(cp);
  1112.             return(FALSE);
  1113.         }
  1114.                 /* ok, so.. it passed on back. Try this again. */
  1115.         if(mkdir(newpath))
  1116.         {
  1117.             free(cp);
  1118.             return(FALSE);
  1119.         }
  1120.     }
  1121.  
  1122.     free(cp);
  1123.     return(TRUE);
  1124. }
  1125.  
  1126. /*--------------------------------------------------- strstr() --------------
  1127. / find a substring within a string
  1128. /---------------------------------------------------------------------------*/
  1129. #ifndef OS2
  1130. char *
  1131. strstr(s1, s2)
  1132. register char *s1, *s2;
  1133. {
  1134.     register int l;
  1135.  
  1136.     if (l = strlen(s2))
  1137.         for ( ; s1 = strchr(s1, s2[0]); s1++)
  1138.             if (memcmp(s1, s2, l) == 0)
  1139.                 break;
  1140.     return(s1);
  1141. }
  1142.  
  1143. /*--------------------------------------------------- strlwr() ---------------
  1144. / strlwr.c - convert passed string to its equivalent lowercases
  1145. /----------------------------------------------------------------------------*/
  1146. char *
  1147. strlwr(s)
  1148. register char *s;
  1149. {
  1150.     char *op;
  1151.  
  1152.     for (op = s; *s; s++)
  1153.         if (isupper(*s))
  1154.             *s = _tolower(*s);
  1155.     return(op);
  1156. }
  1157. #endif
  1158.  
  1159. /*--------------------------------------------------- asciifile() -----------
  1160. / check if passed file is an ascii file using file(1) command
  1161. /---------------------------------------------------------------------------*/
  1162. int
  1163. asciifile(fn)
  1164. char *fn;
  1165. {
  1166.     char cmdstr[256];
  1167.     register FILE *fp;
  1168.  
  1169. #ifdef OS2
  1170.         int file, cnt, i;
  1171.  
  1172.     if ((file = open(fn, O_RDONLY|O_BINARY)) == -1)
  1173.         return(FALSE);
  1174.     cnt = read(file, cmdstr, sizeof(cmdstr)); /* get a block. */
  1175.     close(file);    /* and done. */
  1176.  
  1177.         for ( i = 0; i < cnt; i++ )
  1178.                 if ( cmdstr[i] == 0 || cmdstr[i] == 127 )
  1179.                     return(FALSE);
  1180.  
  1181.         return(TRUE);
  1182. #else /* OS2 */
  1183.     (void) sprintf(cmdstr, "file %s", fn);
  1184.     if (!(fp = popen(cmdstr, "r")))
  1185.         return(FALSE);
  1186.     (void) strrd(ftypestr, 80, fp); /* get a line. */
  1187.     (void) pclose(fp);    /* and done. */
  1188. #ifdef DEBUG
  1189.     (void) printf("%s\n", cmdstr);
  1190. #endif
  1191.     if (strstr(ftypestr, "text"))
  1192.         return(TRUE);
  1193.     return(FALSE);
  1194. #endif /* OS2 */
  1195. }
  1196.  
  1197. /*--------------------------------------------------- rcsfile() -------------
  1198. / check if passed file is an RCS file using file(1) command
  1199. /---------------------------------------------------------------------------*/
  1200. int
  1201. rcsfile(fn)
  1202. char *fn;
  1203. {
  1204.     char cmdstr[256];
  1205.     register FILE *fp;
  1206.  
  1207. #ifdef OS2
  1208.     if (!(fp = fopen(fn, "r")))
  1209.         return(FALSE);
  1210.     (void) strrd(ftypestr, 80, fp); /* get a line. */
  1211.     (void) fclose(fp);    /* and done. */
  1212. #ifdef DEBUG
  1213.     (void) printf("%s\n", cmdstr);
  1214. #endif
  1215.     if (strcmp(ftypestr, "head     "))
  1216.         return(TRUE);
  1217. #else /* OS2 */
  1218.     (void) sprintf(cmdstr, "file %s", fn);
  1219.     if (!(fp = popen(cmdstr, "r")))
  1220.         return(FALSE);
  1221.     (void) strrd(ftypestr, 80, fp); /* get a line. */
  1222.     (void) pclose(fp);    /* and done. */
  1223. #ifdef DEBUG
  1224.     (void) printf("%s\n", cmdstr);
  1225. #endif
  1226. #ifdef V_RCS
  1227.     if (strstr(ftypestr, "text"))
  1228. #else
  1229.     if (strstr(ftypestr, "sccs"))
  1230. #endif
  1231.         return(TRUE);
  1232. #endif /* OS2 */
  1233.     return(FALSE);
  1234. }
  1235.  
  1236. /*--------------------------------------------------- strrd() ----------------
  1237. / read from given file pointer until a line separator or end-of-file or
  1238. / (len) characters excluding the terminator.
  1239. /---------------------------------------------------------------------------*/
  1240. int
  1241. strrd(buf, len, fle)
  1242. char *buf;
  1243. int len;
  1244. FILE *fle;
  1245. {
  1246.     int c0, i0 = 0;
  1247.  
  1248.     while (((c0 = getc(fle)) != EOF) && c0 && c0 != '\n')
  1249.         if(i0 < len)  /* if room in buffer..*/
  1250.             buf[i0++] = (char) c0;  /* save it. */
  1251.     buf[i0] = 0;
  1252.     if (i0 == 0 && c0 == EOF)
  1253.         return(-1);
  1254.     return(i0);
  1255. }
  1256.  
  1257. /*--------------------------------------------------- getdir() --------------
  1258. / get and readin variables for later.
  1259. /---------------------------------------------------------------------------*/
  1260. void
  1261. getdir()
  1262. {
  1263.     register char *cp;
  1264.  
  1265.     if(cp = getenv("HOME"))        /* get user's home dir. */
  1266.     {
  1267.         (void) strcpy(homedir = memalloc(strlen(cp) + 2), cp);
  1268.         if (!(s_homedir = fix_envstr(homedir)))
  1269.         {
  1270.             free(homedir);
  1271.             homedir = (char *) 0;
  1272.         }
  1273.     }
  1274.     else                /* this should NEVER happen */
  1275.     {
  1276.         (void) fprintf(stderr, "No home directory???\n");
  1277.         exit(-1);
  1278.     }
  1279.  
  1280. #ifdef V_RCS
  1281.     if(cp = getenv("RCSDIR"))    /* RCS directory */
  1282. #else
  1283.     if(cp = getenv("SCCSDIR"))    /* SCCS directory */
  1284. #endif
  1285.         (void) strcpy(rcsdir = memalloc(strlen(cp) + 2), cp);
  1286.     else                /* RCS is $HOME/RCS */
  1287.         (void) sprintf(rcsdir = memalloc(s_homedir + 6),
  1288. #ifdef V_RCS
  1289.                 "%s/RCS", homedir);
  1290. #else
  1291.                 "%s/SCCS", homedir);
  1292. #endif
  1293.     if (!(s_rcsdir = fix_envstr(rcsdir)))
  1294.     {
  1295.         free(rcsdir);
  1296.         rcsdir = (char *) 0;
  1297.     }
  1298.  
  1299. #ifdef V_RCS
  1300.     if(cp = getenv("RCSWORK"))    /* user's working directory */
  1301. #else
  1302.     if(cp = getenv("SCCSWORK"))    /* user's working directory */
  1303. #endif
  1304.     {
  1305.         (void) strcpy(rcswrk = memalloc(strlen(cp) + 2), cp);
  1306.         if (!(s_rcswrk = fix_envstr(rcswrk)))
  1307.         {
  1308.             free(rcswrk);
  1309.             rcswrk = (char *) 0;
  1310.         }
  1311.     }
  1312.  
  1313. #ifdef V_RCS
  1314.     if (cp = getenv("RCSSRC"))    /* master source directory */
  1315. #else
  1316.     if (cp = getenv("SCCSSRC"))    /* master source directory */
  1317. #endif
  1318.     {
  1319.         (void) strcpy(srcdir = memalloc(strlen(cp) + 2), cp);
  1320.         if (!(s_srcdir = fix_envstr(srcdir)))
  1321.         {
  1322.             free(srcdir);
  1323.             srcdir = (char *) 0;
  1324.         }
  1325.     }
  1326.  
  1327. #ifdef V_RCS
  1328.     if (cp = getenv("RCSHEAD"))    /* RCS header file directory */
  1329. #else
  1330.     if (cp = getenv("SCCSHEAD"))    /* SCCS header file directory */
  1331. #endif
  1332.     {
  1333.         (void) strcpy(headdir = memalloc(strlen(cp) + 2), cp);
  1334.         if (!(s_headdir = fix_envstr(headdir)))
  1335.         {
  1336.             free(headdir);
  1337.             headdir = homedir;
  1338.         }
  1339.     }
  1340.     else
  1341.         headdir = homedir;
  1342.  
  1343. #ifdef V_RCS
  1344.     if (cp = getenv("RCSOWN"))    /* the owner of RCS files */
  1345. #else
  1346.     if (cp = getenv("SCCSOWN"))    /* the owner of SCCS files */
  1347. #endif
  1348.     {
  1349.         s_rcsown = strlen(cp);
  1350.         (void) strcpy(rcsown = memalloc(s_rcsown + 1), cp);
  1351.     }
  1352.  
  1353.     if(cp = getenv("PATH"))        /* current path, ie. $PATH */
  1354.     {
  1355.         s_path = strlen(cp);
  1356.         (void) strcpy(path = memalloc(s_path + 1), cp);
  1357.     }
  1358.  
  1359.     if((currentdir = getcwd((char *)NULL, 200)) == NULL)
  1360.     {
  1361.         (void) fprintf(stderr, "Cannot get working dir.\n");
  1362.         exit(-1);
  1363.     }
  1364. #ifdef OS2
  1365.         /* strcpy(currentdir, currentdir + 2); */
  1366.         UnixFileName(currentdir);
  1367. #endif
  1368.         strcat(currentdir, "/");
  1369.     s_currentdir = strlen(currentdir);
  1370. }
  1371.  
  1372. /*--------------------------------------------------- fix_envstr() ----------
  1373. / fix environment variable to avoid problems later.
  1374. / 1. strip leading/trailing white spaces
  1375. / 2. strip duplicate slashes
  1376. / 3. add trailing slash
  1377. / 4. return string length
  1378. /---------------------------------------------------------------------------*/
  1379. int
  1380. fix_envstr(cs)
  1381. char *cs;
  1382. {
  1383.     register char *cp, *dp;
  1384.     register int was_slash = FALSE;
  1385.  
  1386. #ifdef OS2
  1387.         UnixFileName(cs);
  1388. #endif
  1389.  
  1390.     cp = dp = cs;
  1391.     while (isspace(*cp))        /* remove leading white spaces */
  1392.         cp++;
  1393.  
  1394.     if (!*cp)            /* string was a full of blanks */
  1395.         return(0);
  1396.  
  1397.     while (*cp)
  1398.     {
  1399.         if (*cp == '/')
  1400.         {
  1401.             if (was_slash)
  1402.             {
  1403.                 cp++;    /* strip duplicate slashes */
  1404.                 continue;
  1405.             }
  1406.             else
  1407.                 was_slash = TRUE;
  1408.         }
  1409.         else
  1410.             was_slash = FALSE;
  1411.         *dp++ = *cp++;
  1412.     }
  1413.  
  1414.     do                /* remove trailing while spaces */
  1415.     {
  1416.         dp--;
  1417.     } while (isspace(*dp));
  1418.  
  1419.     if (*dp != '/')            /* add trailing slash */
  1420.         *++dp = '/';
  1421.     *++dp = '\0';            /* null terminate */
  1422.     return(strlen(cs));
  1423. }
  1424.  
  1425. /*--------------------------------------------------- getrcsdir() -----------
  1426. / get $RCSDIR + tail directory
  1427. /---------------------------------------------------------------------------*/
  1428. void
  1429. getrcsdir(tdir, sdir)
  1430. char *tdir, *sdir;
  1431. {
  1432.     register char *cp = sdir;
  1433.  
  1434.     if(rcswrk && !strnicmp(rcswrk, cp, s_rcswrk))
  1435.         cp += s_rcswrk;
  1436.     if(homedir && !strnicmp(homedir, cp, s_homedir))
  1437.         cp += s_homedir;
  1438. #ifdef OS2
  1439.         if(isalpha(cp[0]) && cp[1] == ':')
  1440.                 cp += 2;
  1441. #endif
  1442.         if(cp[0] == '/')
  1443.                 cp++;
  1444.     /*
  1445.      * build the final directory name
  1446.      */
  1447.     (void) sprintf(tdir, "%s%s", rcsdir, cp);
  1448. }
  1449.  
  1450. /*--------------------------------------------------- getworkdir() ----------
  1451. / get $RCSWORK or $HOME + tail
  1452. /---------------------------------------------------------------------------*/
  1453. void
  1454. getworkdir(tdir, sdir)
  1455. char *tdir, *sdir;
  1456. {
  1457.     register char *cp = sdir;
  1458.  
  1459.     if (rcsdir && !strnicmp(rcsdir, cp, s_rcsdir))
  1460.         cp += s_rcsdir;
  1461. #ifdef OS2
  1462.         if(isalpha(cp[0]) && cp[1] == ':')
  1463.                 cp += 2;
  1464. #endif
  1465.         if(cp[0] == '/')
  1466.                 cp++;
  1467.     (void) sprintf(tdir, "%s%s", rcswrk ? rcswrk : homedir, cp);
  1468. }
  1469.  
  1470. /*--------------------------------------------------- getsrcdir() -----------
  1471. / get $RCSSRC + tail
  1472. /---------------------------------------------------------------------------*/
  1473. void
  1474. getsrcdir(tdir, sdir)
  1475. char *tdir, *sdir;
  1476. {
  1477.     register char *cp = sdir;
  1478.  
  1479.     if (rcswrk && !strnicmp(rcswrk, cp, s_rcswrk))
  1480.         cp += s_rcswrk;
  1481.     if(homedir && !strnicmp(homedir, cp, s_homedir))
  1482.         cp += s_homedir;
  1483. #ifdef OS2
  1484.         if(isalpha(cp[0]) && cp[1] == ':')
  1485.                 cp += 2;
  1486. #endif
  1487.         if(cp[0] == '/')
  1488.                 cp++;
  1489.     (void) sprintf(tdir, "%s%s", srcdir ? srcdir : homedir, cp);
  1490. }
  1491.  
  1492. /*--------------------------------------------------- getfinput() ------------
  1493. / get a title file.
  1494. /---------------------------------------------------------------------------*/
  1495. int
  1496. getfinput(name, type)
  1497. char *name;        /* buffer to put file name into. */
  1498. int type;        /* what data we want. */
  1499. {
  1500.     int stat_loc;
  1501.  
  1502.     (void) strcpy(name, tmpnam(editfile));
  1503. #ifndef OS2
  1504.     if (fork())
  1505.     {    /* parent just waits for the child to finish */
  1506.         (void) wait(&stat_loc);
  1507.     }
  1508.     else
  1509.     {    /* child does his/her stuff */
  1510.         (void) signal(SIGINT, SIG_DFL);
  1511.         (void) signal(SIGQUIT, SIG_DFL);
  1512.         (void) signal(SIGBREAK, SIG_DFL);
  1513.         (void) signal(SIGTERM, SIG_DFL);
  1514.         exit(child_getfinput(name, type) == TRUE ? 0 : -1);
  1515.     }
  1516.     return((stat_loc >> 8) & 0xff);
  1517. #else
  1518.     return child_getfinput(name, type) == TRUE ? 0 : -1;
  1519. #endif
  1520. }
  1521.  
  1522. /*--------------------------------------------------- child_getfinput() -----
  1523. / actual get title file.
  1524. /---------------------------------------------------------------------------*/
  1525. int
  1526. child_getfinput(name, type)
  1527. char *name;        /* buffer to put file name into. */
  1528. int type;        /* what data we want. */
  1529. {
  1530.     static char *input_type[2] = { "log", "title" };
  1531.     FILE *fp, *xfp;
  1532.     register char *st;
  1533.     int c, done = FALSE;
  1534.     char buf[82];            /* just larger than input buffer. */
  1535.                   
  1536. #ifndef OS2
  1537.     (void) setuid(real_user_id);        /* user's real user id */
  1538. #endif        
  1539.     if((fp = fopen(name, "w")) == NULL) /* failed open. */
  1540.     {
  1541.         (void) unlink(name);        /* remove it. */
  1542.         name[0] = '\0';
  1543.         (void) printf("Unable to create tmp file.\n");
  1544.         return(FALSE);
  1545.     }
  1546.     (void) printf("Enter %s message, <ret>.<ret> or Control-Z to end:\n",
  1547.         input_type[type]);
  1548.     while (!done)
  1549.     {
  1550.         (void) printf(">>");
  1551.         if(strrd(buf, 80, stdin) == -1) /* read in one line. */
  1552.         {
  1553.             /* ok, read somewhere that this is possible.  By doing this,
  1554.                we should be able to continue after a control-D.
  1555.             */
  1556.             clearerr(stdin);
  1557.             break;
  1558.         }
  1559.         if(!strcmp(".", buf))    /* end of message */
  1560.             break;
  1561.         if (buf[0] == '~')    /* special command */
  1562.         {
  1563.             switch (buf[1])    /* command character */
  1564.             {
  1565.             case '?':    /* print usage, help message */
  1566.                 (void) printf("\n");
  1567.                 (void) printf("~.    End of input\n");
  1568.                 (void) printf("~!    Invoke shell\n");
  1569.                 (void) printf("~e    Edit message using an editor\n");
  1570.                 (void) printf("~p    Print message buffer\n");
  1571.                 (void) printf("~r    Read in a file\n");
  1572.                 (void) printf("~w    Write message to a file\n");
  1573.                 (void) printf("~?    Print this message\n");
  1574.                 (void) printf("\n");
  1575.                 break;
  1576.             case '!':    /* shell */
  1577.                 st = getenv("SHELL");
  1578.                 (void) system(st ? st : "/bin/sh");
  1579.                 (void) printf("[Press RETURN to continue]");
  1580.                 (void) strrd(buf, 20, stdin);
  1581.                 break;
  1582.             case 'p':    /* print message buffer content */
  1583.                 (void) fclose(fp);
  1584.                 fp = fopen(name, "r");
  1585.                 while ((c = fgetc(fp)) != EOF)
  1586.                     (void) fputc(c, stdout);
  1587.                 (void) fclose(fp);
  1588.                 fp = fopen(name, "a");
  1589.                 (void) printf("Continue entering %s message.\n",
  1590.                     input_type[type]);
  1591.                 break;
  1592.             case 'e':    /* editor */
  1593.             case 'v':    /* visual */
  1594.                 (void) fclose(fp);
  1595.                 st = getenv(buf[1] == 'e' ?"EDITOR":"VISUAL");
  1596.                 (void) sprintf(buf, "%s %s",
  1597.                     st ? st : "/usr/bin/vi", name);
  1598.                 (void) system(buf);
  1599.                 fp = fopen(name, "a");
  1600.                 (void) printf("Continue entering %s message.\n",
  1601.                     input_type[type]);
  1602.                 break;
  1603.             case 'r':    /* read in a file */
  1604.                 st = &buf[2];
  1605.                 while (isspace(*st))
  1606.                     st++;
  1607.                 if (!*st)
  1608.                 {
  1609.                     (void) printf("File name missing!\n");
  1610.                     break;
  1611.                 }
  1612.                 if (xfp = fopen(st, "r"))
  1613.                 {
  1614.                     while ((c = fgetc(xfp)) != EOF)
  1615.                         (void) fputc(c, fp);
  1616.                     (void) fclose(xfp);
  1617.                 }
  1618.                 else
  1619.                     (void) printf("Unable to open %s.\n",
  1620.                             st);
  1621.                 break;
  1622.             case 'w':    /* write message to a file */
  1623.                 st = &buf[2];
  1624.                 while (isspace(*st))
  1625.                     st++;
  1626.                 if (!*st)
  1627.                     (void) printf("File name missing!\n");
  1628.                 else
  1629.                 {
  1630.                     if (xfp = fopen(st, "a"))
  1631.                     {
  1632.                         (void) fclose(fp);
  1633.                         fp = fopen(name, "r");
  1634.                         while ((c = fgetc(fp)) != EOF)
  1635.                             (void) fputc(c, xfp);
  1636.                         (void) fclose(xfp);
  1637.                         (void) fclose(fp);
  1638.                         fp = fopen(name, "a");
  1639.                     }
  1640.                     else
  1641.                         (void) printf("Unable to open %s.\n",
  1642.                             st);
  1643.                 }
  1644.                 break;
  1645.             case '.':    /* end of message */
  1646.                 done = TRUE;
  1647.                 break;
  1648.             default:    /* user doesn't know */
  1649.                 (void) printf("Unrecognized command %c -- ignored\n",
  1650.                     buf[1] & 0x7f);
  1651.                 break;
  1652.             }
  1653.             continue;
  1654.         }
  1655.         (void) fprintf(fp, "%s\n", buf);
  1656.     }
  1657.     (void) fclose(fp);
  1658.     (void) printf("\n");
  1659.     return(TRUE);
  1660. }
  1661.  
  1662. /*--------------------------------------------------- justname() ------------
  1663. / extract just filename from a full path
  1664. /---------------------------------------------------------------------------*/
  1665. char *
  1666. justname(fpath)
  1667. char *fpath;
  1668. {
  1669.     register char *cp;
  1670.  
  1671. #ifdef OS2
  1672.         UnixFileName(fpath);
  1673. #endif
  1674.     if (cp = strrchr(fpath, '/'))
  1675.         return(++cp);
  1676.  
  1677.     return(fpath);
  1678. }
  1679.  
  1680. /*--------------------------------------------------- memalloc() ------------
  1681. / allocate specified amount of memory.  If not successful, exit.
  1682. /---------------------------------------------------------------------------*/
  1683. char *
  1684. memalloc(size)
  1685. register int size;
  1686. {
  1687.     register char *cp;
  1688.  
  1689.     if (!(cp = malloc((unsigned)size)))
  1690.     {
  1691.         perror(prognam);
  1692.         exit(99);
  1693.     }
  1694.     return(cp);
  1695. }
  1696.  
  1697. /*--------------------------------------------------- get_final_id() --------
  1698. / Get the RCSOWN user id to create files with.  If none found, use user id.
  1699. /---------------------------------------------------------------------------*/
  1700. void
  1701. get_final_id()
  1702. {
  1703.     FILE *fp;
  1704.  
  1705.     if(!rcsown)            /* if there isn't one of these. */
  1706. #ifdef V_RCS
  1707.         rcsown = "rcsfiles";    /* default name. */
  1708. #else
  1709.         rcsown = "sccsfiles";    /* default name. */
  1710. #endif
  1711.  
  1712.     if ((fp = fopen (pwdfile, "r")) == NULL)
  1713.     {
  1714. #ifdef DEBUG
  1715.         (void) fprintf(stderr, "setpwent: %s non-existant or unreadable.\n",
  1716.                 pwdfile);
  1717. #endif
  1718.         user_id = real_user_id;        /* make sure it's owners id now. */
  1719.         return;        /* couldn't do it. */
  1720.     }
  1721.     while (nextent(fp))        /* while entries in file.. */
  1722.         if (!strcmp(pwdname, rcsown))   /* If name matches. */
  1723.             break;
  1724.     (void) fclose(fp);    /* close the file. */
  1725.     return;            /* found it or not, return. */
  1726. }
  1727.  
  1728. /*--------------------------------------------------- nextent() -------------
  1729. / get one entry from a password file.  Return TRUE if there is one found,
  1730. / FALSE otherwise.
  1731. /---------------------------------------------------------------------------*/
  1732. int
  1733. nextent(fle)
  1734. FILE *fle;     /* file pointer. */
  1735. {
  1736.     register char *cp, *pwp;
  1737.     char savbuf[200];    /* usually large enough for a password entry. */
  1738.  
  1739.     while (strrd(savbuf, (int) (sizeof (savbuf)), fle) != -1)
  1740.     {
  1741.         pwp = pwdname;
  1742.         cp = savbuf;        /* get user name */
  1743.         while (*cp && *cp != ':')
  1744.             *pwp++ = *cp++;
  1745.         *pwp = '\0';        /* terminate name. */
  1746.         for (cp++; *cp && *cp != ':'; cp++)
  1747.             ;        /* skip over password. */
  1748.         user_id = atoi(++cp);    /* ok, save this users id number. */
  1749.         return (TRUE);
  1750.     }
  1751.     user_id = real_user_id;        /* make sure it's owners id now. */
  1752.     return (FALSE);
  1753. }
  1754.  
  1755. /*----------------------------- End of cio.c -------------------------------*/
  1756.  
  1757. #ifdef OS2
  1758. UnixFileName(char *name)
  1759. {
  1760.   /* strlwr(name); */
  1761.   for ( ; *name; name++ )
  1762.     if ( *name == '\\' )
  1763.       *name = '/';
  1764. }
  1765. #endif
  1766.  
  1767.